package Arrays;

/**
 * 作者:hfj
 * 功能:338.比特位计数
 * 日期: 2025/11/13 10:19
 */
public class CountBits {

    public static void main(String[] args) {
        System.out.println(countBits(5));
    }

    public static int[] countBits(int n) {

        int[] tar = new int[n+1];
        tar[0] = 0;
        String temp;
        for (int i = 1; i <= n; i++) {
            temp = "";
            int count = 0,k = i;
            while (k > 0){
               temp += (k % 2);
               k /= 2;
            }
            for (int j = 0; j < temp.length(); j++) {
                if (temp.charAt(j) == '1'){
                    count++;
                }
            }
            tar[i] = count;
        }
        return tar;
    }
}
